package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import org.springframework.beans.factory.annotation.Autowired;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.order.base.consts.KeyValues;

/**
 * 细分业务类型属性处理器
 */
public class BusiSegmentTypeAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private OperateInstAttrService operateInstAttrService;
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.set(attr, inst, value, data);
        Map<String, Object> extParameter = querySceneInstService.getExtParameter(data.getSceneInstId());
        if(KiteMapUtils.isNotEmpty(extParameter)){
            String id = KiteMapUtils.getString(extParameter,"sceneTempl");
            if(!KiteStringUtils.isEmpty(id)){
                dealOperTypeAttr(inst);
                return;
            }
        }
        //变更业务的场景初始化过程中不校验
        if (!KiteStringUtils.equalsAny(data.getServiceOfferId(), LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK, ServiceOfferConsts.SERVICE_OFFER_ORDER)
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), "initSource"))) {
            // 变更业务 细分业务类型不可编辑
            if (KiteStringUtils.equalsAny(value, "100006958", "100006959", "100006960", "100006961", "100006962", "100006963") && attr.getLoadFlag()) {
                attr.setIsEdit(KeyConsts.IFTRUE_F);
            }
            return;
        }

        dealOperTypeAttr(inst);

        //重提初始化不校验
        //fixme 可能导致频繁访问缓存,上线临时处理方案
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (KiteMapUtils.isNotEmpty(extParams) && extParams.containsKey("nodeCode")) {
            return;
        }
        List<KiteAttr> attrs = inst.listAttrs();
        attrs.forEach(kiteAttr->{
            if(!KiteStringUtils.isEqual(kiteAttr.getFieldName(),attr.getFieldName())
                && !KiteStringUtils.isEqual(kiteAttr.getFieldName(),"200003996")
                && !KiteStringUtils.isEqual(kiteAttr.getFieldName(),"200003999")
                && !KiteStringUtils.isEqual(KeyConsts.PROD_ATTR_SHOW_TYPE_E,kiteAttr.getShowType())){
                List<KiteAttrValue> attrValues =  kiteAttr.getAttrValues();
                String attrType = kiteAttr.getAttrValueTypeId();
                if(KiteListUtils.isNotEmpty(attrValues) && !KiteStringUtils.equals(attrType, KeyValues.INPUT)){
                    String defaultValue = kiteAttr.getDefaultValue();
                    List<KiteAttrValue> filterAttrValues = attrValues.stream().filter(AttrValue -> KiteStringUtils.isEqual(AttrValue.getAttrValue(),defaultValue)).collect(Collectors.toList());
                    KiteAttrValue attrValue = attrValues.get(0);
                    if(KiteStringUtils.isEmpty(defaultValue) || KiteListUtils.isEmpty(filterAttrValues)){
                        operateInstAttrService.setAttrValue(inst, kiteAttr.getFieldName(), attrValue.getAttrValue(), data.getSceneInstId(), null);
                    }else{
                        operateInstAttrService.setAttrValue(inst, kiteAttr.getFieldName(), kiteAttr.getDefaultValue(), data.getSceneInstId(), null);
                    }
                }else{
                    operateInstAttrService.setAttrValue(inst, kiteAttr.getFieldName(), kiteAttr.getDefaultValue(), data.getSceneInstId(), null);
                }
            }
        });
    }

    private void dealOperTypeAttr(KiteInst inst) {
        KiteAttr operTypeAttr = inst.getAttr("200002753");
        String apnType = inst.getAttrValue("200003999");
        String busiType = inst.getAttrValue("200004001");
        List<AttrValue> operTypes = AttrValueCache.get(operTypeAttr.getAttrId());
        // 新增，展示新装
        if (KiteStringUtils.equals(ActionType.A.name(), inst.getActionType().name())) {
            //operTypes.removeIf(_attr -> !KiteStringUtils.equals(_attr.getAttrValue(), "100004575"));
            /*operTypeAttr.setAttrValues(KiteBeanUtils.copyPropsForList(operTypes, KiteAttrValue.class));
            if (KiteStringUtils.equals(apnType, "100003262") && KiteStringUtils.equals(busiType, "100006958")) {
                operTypes.removeIf(_attr -> !KiteStringUtils.equalsAny(_attr.getAttrValue(), "100004575"));
            }*/
            operTypes.removeIf(attr -> !KiteStringUtils.equalsAny(attr.getAttrValue(), "100004575"));
        }
        else if (KiteStringUtils.equals(ActionType.K.name(), inst.getActionType().name())
            || KiteStringUtils.equals(ActionType.M.name(), inst.getActionType().name())) {
            //operTypes.removeIf(_attr -> !KiteStringUtils.equals(_attr.getAttrValue(), "100004575"));
            operTypeAttr.setAttrValues(KiteBeanUtils.copyPropsForList(operTypes, KiteAttrValue.class));
            if (KiteStringUtils.equals(apnType, "100003262") && KiteStringUtils.equalsAny(busiType, "100006958", "100006959")) {
                operTypes.removeIf(attr -> !KiteStringUtils.equalsAny(attr.getAttrValue(), "100004575", "100007391"));
            }
            else if (KiteStringUtils.equals(apnType, "100006957") && KiteStringUtils.equalsAny(busiType, "100006960")) {
                operTypes.removeIf(attr -> KiteStringUtils.equalsAny(attr.getAttrValue(), "100007391"));
            }
            else if (KiteStringUtils.equals(apnType, "100006957") && KiteStringUtils.equalsAny(busiType, "100006961")) {
                operTypes.removeIf(attr -> !KiteStringUtils.equalsAny(attr.getAttrValue(), "100004575", "100007395"));
            }
            else if (KiteStringUtils.equals(apnType, "100006957") && KiteStringUtils.equalsAny(busiType, "100006962")) {
                operTypes.removeIf(attr -> KiteStringUtils.equalsAny(attr.getAttrValue(), "100007391", "100007395"));
            }
            else if (KiteStringUtils.equals(apnType, "100006957") && KiteStringUtils.equalsAny(busiType, "100006963")) {
                operTypes.removeIf(attr -> !KiteStringUtils.equalsAny(attr.getAttrValue(), "100004575"));
            }
        }
        operTypeAttr.setAttrValues(KiteBeanUtils.copyPropsForList(operTypes, KiteAttrValue.class));
    }
}
